package models;

import models.msgs.*;
import models.msgs.Call;
import models.msgs.Number;
import play.mvc.*;
import play.libs.*;
import play.libs.F.*;
import akka.actor.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class Human extends UntypedActor
{
    public final String               name;
    public final ActorRef             table;
    public int                        numer;

    protected WebSocket.In<JsonNode>  in;
    protected WebSocket.Out<JsonNode> out;

    
    public Human(String _name, WebSocket.In<JsonNode> _in,
            WebSocket.Out<JsonNode> _out, ActorRef _table, int _numer)
    {
        name = _name;        
        table = _table;
        in = _in;
        out = _out;
        numer = _numer;
        

        in.onMessage(new Callback<JsonNode>()
        {
            public void invoke(JsonNode event)
            {
              //Obsluga przycisku Send Cards 
                try {
                	String kod=event.get("nr").toString();
                	Decode.check(kod);
                	if(Decode.getInfo()){          	
	                	table.tell(new Number(numer,name,event.get("nr").toString()), getSelf() ); 
                	}
                	else
                	{
                		getSelf().tell(new Info("Wprowadzono błędne dane",name), getSelf() ); 	
                	}
                	return;
                	
                }catch (Exception e) {}
              //Obsluga przycisku Raise 
                try
                {
                	Decode.checkInt(event.get("raise").toString()); 
                	if(Decode.getInfo()){ 
	                	int kasa = Decode.getInt();
	                	table.tell(new Raise(numer,name, kasa), getSelf() );   
                	}
                	else
                	{
                		getSelf().tell(new Info("Wprowadzono błędne dane",name), getSelf() ); 	
                	}
                	return;
                }catch (Exception e) {}
              //Obsluga przycisku Bet 
                try
                {
                	Decode.checkInt(event.get("bet").toString());    
                	if(Decode.getInfo()){ 
	                	int kasa = Decode.getInt();                
	                	table.tell(new Bet(numer,name,kasa), getSelf()); 
                	}
                	else
                	{
                		getSelf().tell(new Info("Wprowadzono błędne dane",name), getSelf() ); 	
                	}
                	return;
                }catch (Exception e) {}
              //Obsluga przycisku Start 
                try
                {
                	event.get("start").isInt();                
                	table.tell(new Init(numer,name), getSelf() );  
                	return;
                }catch (Exception e) {}
              //Obsluga przycisku Check 
                try
                {
                	event.get("check").isInt();                 
                	table.tell(new Check(numer,name), getSelf() );      
                	return;
                }catch (Exception e) {}
              //Obsluga przycisku Call 
                try
                {
                	event.get("cal").isInt();                
                	table.tell(new Call(numer,name), getSelf() );  
                	return;
                }catch (Exception e) {}
              //Obsluga przycisku Fold 
                try
                {
                	event.get("fold").isInt();                
                	table.tell(new Fold(numer,name), getSelf() );     
                	return;
                } catch (Exception e) {}
              //Obsluga przycisku All-in 
                try
                {
                	event.get("allin").isInt();                
                	table.tell(new Allin(numer,name), getSelf() );   
                	return;
                } catch (Exception e) {}
                
                
            }
        });

        in.onClose(new Callback0()
        {
            public void invoke()
            {
                table.tell(new Quit(name,numer), getSelf() );
            }
        });
    }
    
    @Override
    public void preStart()
    {        
        String text = "my Actor is now running!";			//Co z tym mozna jeszcze zrobic?
        table.tell(new Info(text, name), getSelf()); 
    }

    @Override
    public void postStop()
    {
        String text = "I've been killed";
    	table.tell(new Info(text, name), getSelf()); 
    }

    @Override
    public void onReceive(Object msg) throws Exception
    {
 
            if (msg instanceof Number)
            {   
            	table.tell(new Win(numer,name), getSelf()); 
            }  
            else if (msg instanceof Card)//?
            {
            	Card card = (Card) msg;    
            	ObjectNode event = Json.newObject();
            	event.put("message","I've got " + card.getRank() + " " + card.getColor() + " from Table" );
            	out.write(event); 
            }            
            
            else if(msg instanceof Info)
            {        
            	Info info = (Info) msg;             	
	            ObjectNode event = Json.newObject();
	            event.put("message", "[ "+ info.getName()+ " ] : " + info.getText()); 
	            out.write(event);
            }
            else if(msg instanceof Cash)
            {        
            	Cash cash = (Cash) msg;         	
	            ObjectNode event = Json.newObject();
	            event.put("message", "[ "+ cash.getName()+ " ] : Moj stan konta wynosi : " + cash.getCash() + ", stawka to : "+cash.getBuyIn());
	            out.write(event);   
            }
            else if(msg instanceof Rozdaj)
            {     
             	table.tell(new Rozdaj(numer), getSelf()); 
            }
            else if(msg instanceof Init)
            {     
            	Init init = (Init) msg; 
            	ObjectNode event = Json.newObject();
            	event.put("message", "[ Table ] : Licytuje gracz : "+init.getNumber());
	            out.write(event);
            }
            else if(msg instanceof Change)
            {     
            	Change change = (Change) msg; 
            	ObjectNode event = Json.newObject();
            	event.put("message", "[ Table ] : Karty wimienia gracz : "+change.getNumber());
	            out.write(event);
            }
            else if(msg instanceof Quit)
            {     
            	ObjectNode event = Json.newObject();
            	event.put("message", "[ "+name+" ] : Koniec gry :(");
	            out.write(event);
	            table.tell(new Quit(name,numer), getSelf());
            }
            else {
                unhandled(msg);
            }
            
        }  
            
}

